<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button type="danger" :disabled="dataList.length <= 0" @click="clearAll()">清空回收站</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="文件id">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="文件类别">
        <template slot-scope="scope">
        <div slot="reference" class="name-wrapper">
          {{scope.row.type == 1 ? "文件夹":"文件"}}
        </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.type == 1" size="small" @click="showFileList(scope.row.id)">查看文件详情</el-button>
          <el-button type="text" v-if="scope.row.type == 1" size="small" @click="restoreDirHandle(scope.row.id)">回收</el-button>
          <el-button type="text" v-if="scope.row.type == 1" size="small" @click="clearDirHandle(scope.row.id)">删除</el-button>
          <el-button type="text" v-if="scope.row.type == 0" size="small" @click="restoreFileHandle(scope.row.id)">回收</el-button>
          <el-button type="text" v-if="scope.row.type == 0" size="small" @click="clearFileHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-drawer
        style="position: absolute;width:80%;top:12%;left:10%"
        title="文件列表"
        :visible.sync="table"
        direction="rtl"
        :before-close="handleClose"
        size="100%">
        <el-table :data="gridData">
            <el-table-column property="fileId" label="文件id"></el-table-column>
            <el-table-column property="fileName" label="文件名称"></el-table-column>
            <el-table-column property="createTime" label="文件创建时间"></el-table-column>
          </el-table>
      </el-drawer>
    </div>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        table: false,
        dialog: false,
        loading: false,
        gridData: [],
        dataForm: {
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/generator/mkdir/autoClear'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
          })
        }).then(()=>{
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/mkdir/deleteList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
          })
         }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })

        })
        
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
     //回收文件
      restoreFileHandle(id) {
        this.$confirm(`确定回收该文件吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/file/reclaim/${id}`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 删除文件
      clearFileHandle (id) {
        this.$confirm(`确定对文件进行删除吗?删除后彻底清除`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/file/clear/${id}`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //回收文件夹
      restoreDirHandle(id) {
        this.$confirm(`确定回收该文件夹吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/mkdir/reclaim/${id}`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 删除文件夹
      clearDirHandle (id) {
        this.$confirm(`确定对文件进行删除吗?删除后彻底清除`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/mkdir/clear/${id}`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //清空回收站
      clearAll() {
        this.$confirm(`确定清空回收站吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/mkdir/clearAll`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      //展示文件夹下文件详情
      showFileList(dirId) {
        this.$http({
            url: this.$http.adornUrl(`/generator/mkdir/getDirFile/${dirId}`),
            method: 'get',
            data: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0){
            this.gridData = data.file;
            }
          })
        this.table = true;
      },
      //关闭至空
      handleClose() {
        this.gridData = [];
        this.table = false;
      }
    }
  }
</script>
